﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace SlideshowGenerator
{
    public partial class Frm_SlideshowCreation : Form
    {
        private Slideshow _slideshow;

        private bool _success = false;
        private Timer _lbl_successTimer = new Timer();
        private Timer _creationStarter = new Timer();

        static readonly OperatingSystem osInfo = Environment.OSVersion;

        internal static bool Windows7OrGreater
        {
            get
            {
                return (osInfo.Version.Major == 6 && osInfo.Version.Minor >= 1)
                    || (osInfo.Version.Major > 6);
            }
        }

        /// <summary>
        /// Form in which the Slideshow Creation process will be displayed.
        /// </summary>
        /// <param name="Slideshow">The Slideshow which is going to be created.</param>
        public Frm_SlideshowCreation(Slideshow Slideshow)
        {
            InitializeComponent();

            _slideshow = Slideshow;

            if (!Windows7OrGreater)
            {
                this.ShowInTaskbar = false;
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (cbx_openFolder.Checked)
            {
                Process.Start(_slideshow.SlideShowPath);
            }

            this.Dispose();
        }

        private void Frm_SlideshowCreation_Load(object sender, EventArgs e)
        {
            //Create Slideshow
            win7progress.Value = 0;
            win7progress.Style = ProgressBarStyle.Blocks;
            win7progress.Maximum = _slideshow.ImagesCount;            
        }

        void _lbl_successTimer_Tick(object sender, EventArgs e)
        {
            _lbl_successTimer.Stop();
            if(_success)
            {
                this.lbl_success.Visible = true;
            }
            else
            {
                this.lbl_success.ForeColor = Color.Red;
                this.lbl_success.Text = Properties.Resources.text_slideshowCreationError;
                this.lbl_success.Visible = true;
            }
            this.cbx_openFolder.Enabled = true;
            this.btn_ok.Enabled = true;
        }

        private void Frm_SlideshowCreation_Shown(object sender, EventArgs e)
        {
            //Start creation
            _creationStarter.Interval = 610;
            _creationStarter.Tick += new EventHandler(_crationStarter_Tick);
            _creationStarter.Start();
        }

        void _crationStarter_Tick(object sender, EventArgs e)
        {
            //Do creation
            _creationStarter.Stop();
            _success = _slideshow.CreateSlideShow(win7progress);
            _lbl_successTimer.Interval = 610;
            _lbl_successTimer.Tick += new EventHandler(_lbl_successTimer_Tick);
            _lbl_successTimer.Start();
        }
    }
}
